<template>
  <view>
    <From
      :rules="rules"
      :formSchema="schema"
      class="mt-20rpx"
      @formSubmit="handleSubmit"
    />
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import From from '@/components/Form/FormComponent.vue'
// 获取地址，筛选出默认地址
import { getAddress } from '@/api/address'
import { onLoad } from '@dcloudio/uni-app'
import { addOrder } from '@/api/order'
import { getrepairpriceList } from '@/api/price'
import dayjs from 'dayjs'

const addressList = ref<UserAddress[]>([])
const optionAddress = ref<string[]>([])
const priceList = ref<RepairPrice[]>([])
const optionData = ref<string[]>([])

const schema = ref<IFormSchema[]>([
  {
    field: 'name',
    label: '资产名称',
    type: 'Select',
    cell: '资产报修内容',
    placeholder: '请选择资产',
    options: optionData
  },
  {
    field: 'info',
    label: '损坏说明',
    type: 'Textarea',
    cell: '资产报修内容',
    placeholder: '请输入损坏说明'
  },
  {
    field: 'errorCode',
    label: '故障代码',
    type: 'Input',
    cell: '资产报修内容',
    placeholder: '请输入故障代码'
  },
  {
    field: 'errInfo',
    label: '故障内容',
    type: 'Textarea',
    cell: '资产报修内容',
    placeholder: '请输入故障内容'
  },
  {
    field: 'hopeTime',
    label: '期望维修时间',
    type: 'Time',
    cell: '资产报修内容',
    placeholder: '请选择时间'
  },
  {
    field: 'phone',
    label: '联系方式',
    type: 'Input',
    cell: '资产报修内容',
    placeholder: '请输入联系方式'
  },
  {
    field: 'hopePlace',
    label: '维修地点',
    type: 'Address',
    cell: '资产报修内容',
    placeholder: '请选择期望维修地点',
    options: optionAddress
  },
  {
    field: 'detail',
    label: '备注',
    type: 'Textarea',
    cell: '资产报修内容',
    placeholder: '请输入备注'
  }
])

const rules = ref<any>({
  name: [{ required: true, message: '请选择资产名称', trigger: 'blur' }],
  info: [{ required: true, message: '请输入损坏说明', trigger: 'blur' }],
  errorCode: [{ required: true, message: '请输入故障代码', trigger: 'blur' }],
  errInfo: [{ required: true, message: '请输入故障内容', trigger: 'blur' }],
  hopeTime: [
    { required: true, message: '请选择期望维修时间', trigger: 'blur' }
  ],
  phone: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
  hopePlace: [{ required: true, message: '请选择维修地点', trigger: 'blur' }]
})

const getPrice = (name: string) => {
  return priceList.value.find((item) => item.item_name === name)?.price
}

const handleSubmit = async (e: any) => {
  const address = e.hopePlace.split('-')
  const address_id = addressList.value.find((item) => {
    return item.location === address[0]
  })?.id
  const res = await addOrder<any, RepairOrder>({
    asset_name: e.name,
    damage_desc: e.info,
    fault_code: e.errorCode,
    fault_detail: e.errInfo,
    expected_time: dayjs(e.hopeTime).format('YYYY-MM-DD HH:mm:ss'),
    contact_phone: e.phone,
    repair_address: address[0],
    remark: e.detail,
    address_id: address_id,
    user_id: uni.getStorageSync('user').id,
    price: getPrice(e.name)
  })
  if (res.code === 200) {
    uni.showToast({ title: '报修成功' })
    uni.navigateBack()
  } else {
    uni.showToast({ title: res.message || '报修失败' })
  }
}

onLoad(async () => {
  const res = await getAddress<any, UserAddress>({
    user_id: uni.getStorageSync('user').id
  })
  if (res.code === 200) {
    addressList.value = res.data.items || []
    optionAddress.value = addressList.value.map((item) => {
      return `${item.location}-${item.address_detail}`
    })
    console.log(optionAddress.value)
  }
  const priceRes = await getrepairpriceList<any, RepairPrice>({
    user_id: uni.getStorageSync('user').id
  })
  if (priceRes.code === 200) {
    priceList.value = priceRes.data.items || []
    optionData.value = priceList.value.map((item) => {
      return item.item_name
    })
    console.log(optionData.value)
  }
})
</script>
